﻿using ThunderboltIocFrame.Implement;
using ThunderboltIocFrame.Interface;

namespace ThunderboltIocFrame;

/// <summary>
/// 容器建造者(同时负责注册)
/// </summary>
public class ContainerBuilder
{
	private  static readonly object Locker = new();
	private static ContainerBuilder? _containerBuilder;
	private readonly IServiceCollection _services;

	public static ContainerBuilder GetBuilder ()
	{
		if (_containerBuilder != null) return _containerBuilder;
		lock (Locker)
		{
			_containerBuilder = new ContainerBuilder();
		}
		return _containerBuilder;
	}

	private ContainerBuilder()
	{
		_services = new ServiceCollection();
	}
	public IContainer Build()
	{
		return new Container(_services);
	}
	/// <summary>
	/// 注册瞬态类型
	/// </summary>
	public void RegisterTransient<TService>() where TService : class
	{
		var service = new ServiceDescriptor(typeof(TService), LifeCycle.Transient);
		_services.Add(service);
	}
	/// <summary>
	/// 注册瞬态类型（带继承关系）
	/// </summary>
	public void RegisterTransient<TBaseService, TService>() where TService : class
	{
		var service = new ServiceDescriptor(typeof(TService), typeof(TBaseService), LifeCycle.Transient);
		_services.Add(service);
	}
	/// <summary>
	/// 注册单例类型
	/// </summary>
	public void RegisterSingleton<TService>() where TService : class
	{
		var service = new ServiceDescriptor(typeof(TService), LifeCycle.Singleton);
		_services.Add(service);
	}
	/// <summary>
	/// 注册单例类型（带继承关系）
	/// </summary>
	public void RegisterSingleton<TBaseService, TService>() where TService : class
	{
		var service = new ServiceDescriptor(typeof(TService), typeof(TBaseService), LifeCycle.Transient);
		_services.Add(service);
	}

}